import { createApp } from 'vue';
import VeriftyPwd from './index.vue';
import { VerifyPwdOptOutput } from '/@/api/system/sysAuth';

/**
 * 验密弹框hooks
 * @param props 弹框配置（title/entityId）
 * @returns Promise<VerifyPwdOptOutput> 验密结果
 */
export default function useVeriftyPwd(props: { title?: string; entityId?: string }, userId?: number): Promise<VerifyPwdOptOutput> {
  let instance: any = null;

  const createInstance = () => {
    const app = createApp(VeriftyPwd, {
      title: props.title || '验密',
      entityId: props.entityId || ''
    });
    const container = document.createElement('div');
    instance = app.mount(container);
    document.body.appendChild(container);
  };

  const open = () => {
    if (!instance) createInstance();
    instance.open(userId);
  };

  return new Promise((resolve, reject) => {
    open();
    // 绑定回调
    instance.config = {
      confirmCallback: (res: any) => { resolve(res); instance = null; },
      cancelCallback: () => { reject(); instance = null; },
      destory: () => {
        if (instance) {
          instance.$el.parentNode?.removeChild(instance.$el);
          instance = null;
        }
      }
    };
  });
}